package com.fingard.dsp.bank.directbank.jhbank01;

import java.util.Map;

import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.dsp.bank.directbank.jhbank01.utils.PublickeyEncript;
import com.fingard.net.ITcpClient;
import com.fingard.net.TcpPortClient;
import com.fingard.net.TcpReceiver;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextWriter;

/**
 * Created by F on 2018/7/20.
 */
public class JHBANK01Base extends DirectBase implements ITcpClient{
    @Override
    public void getContentLength(TcpReceiver p_receiver) throws Exception {
        if (p_receiver.contentLength == -1) {
            if (p_receiver.readedLength >= 6) {
              String tmpHead = new String(p_receiver.recBytes, 0, 6, getCharset());
                p_receiver.contentLength = Integer.parseInt(tmpHead.trim());
                p_receiver.contentStartIndex =6;
            }
        }
    }
    protected String getCharset(){
        return "GBK";
    }

    protected String[] sendToBank(String xml) throws Exception {
       xml = String.format("%06d%s", xml.length(), xml);
        WriteBankLogLn("发送给银行的报文明文："+xml);
        Map<String,String> encropyMap = PublickeyEncript.encryptData(xml,getBankConfig().getValueAsString("certificatePath"));
        XmlTextWriter textWriter = new XmlTextWriter();
        textWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\""+getCharset()+"\" ?>");
        textWriter.writeStartElement("root");
        String nodeno = getBankConfig().getValueAsString("nodeNo");
        textWriter.writeElementString("nodeno",nodeno);
        textWriter.writeElementString("encryptkey", encropyMap.get("encryptkey"));
        textWriter.writeElementString("encryptdata", encropyMap.get("encryptdata"));
        textWriter.writeEndDocument();
        xml = textWriter.toXmlString();
        xml = String.format("%06d%s", xml.getBytes(getCharset()).length, xml);
        WriteBankLogStep2(xml);
        WriteBankUrlLogLn(bankFront.getIpPortOrUrl());
        String[] result = {"",""};
        TcpPortClient client = new TcpPortClient(bankFront.serverIP,Integer.valueOf(bankFront.serverPort),this);
        TcpReceiver receiver =client.sendMessage(xml.getBytes(getCharset()));
        result[0] = receiver.connState;
        if(result[0].length()!=0){
           result[1]=receiver.connMsg;
            return result;
        }

        result[1] = new String(receiver.recBytes,0,receiver.contentLength+receiver.contentStartIndex,getCharset());
        WriteBankLogStep3(result[1]);

        if(result[1].length()>6)
        {
            result[1]= result[1].substring(6);
        }
        return result;
    }

    protected void writeCommenHeader(XmlTextWriter xmlTextWriter,JHBANK01Header header){
        xmlTextWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"GBK\" ?>");
        xmlTextWriter.writeStartElement("root");
        xmlTextWriter.writeStartElement("Head");
        xmlTextWriter.writeElementString("userid",header.userid);//操作号
        xmlTextWriter.writeElementString("brchno",header.brchno,true);//柜员
        xmlTextWriter.writeElementString("servtp",header.servtp,true);//渠道
        xmlTextWriter.writeElementString("prcscd",header.prcscd);//交易号
        xmlTextWriter.writeEndElement();
    }

}
